// pages/medical/doctor/index.js
import {
  hospitalApi
} from "../../../utils/hospital-api";
const dayjs = require('../../../utils/dayjs.min.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    dates: [],
    active: 0,
    weeks: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
    doctors: [],
    activeDate: '',
    dateFull: '',
    scheduleData: [],
    amData: [],
    pmData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 保存页面参数信息
    const dept = wx.getStorageSync('dept')
    this.setData({
      hosCode: options.hosCode,
      deptId: options.deptId,
    })
    wx.setNavigationBarTitle({
      title: dept.DEPNAME
    })
    this.initDate();
    this.getSchedule();
  },
  // 预约模式的改变
  onChange(event) {
    const name = event.detail.name;
    this.setData({
      active: name
    })
    if (name == 0) {
      this.getSchedule();
    } else {
      this.getDoctor();
    }
  },
  // 初始化时间数据
  initDate() {
    let dates = [];
    for (let i = 0; i < 8; i++) {
      let date = new Date();
      date.setDate(date.getDate() + i);
      dates.push({
        dateFull: dayjs(date).format('YYYY-MM-DD'),
        date: dayjs(date).format('MM-DD'),
        day: date.getDay()
      });
    }
    this.setData({
      dates: dates,
      activeDate: dates[0].date,
      dateFull: dates[0].dateFull,
    })
  },
  // 时间改变的事件
  changeDate(e) {
    const item = e.mark.item;
    this.setData({
      activeDate: item.date,
      dateFull: item.dateFull,
    })
    wx.setStorageSync('activeDate', item.date)
    wx.setStorageSync('dateFull', item.dateFull)
    this.getSchedule();
  },
  // 根据日期和科室查询医生排版信息
  getSchedule() {
    this.setData({
      loading: true,
      amData: [],
      pmData: [],
    })
    hospitalApi({
      method: 'getSchedule',
      data: JSON.stringify({
        hosCode: this.data.hosCode,
        depId: this.data.deptId,
        startDate: this.data.dateFull,
        endDate: this.data.dateFull,
        isWithSection: '1',
      })
    }).then((res) => {
      let amData = []; //上午排班
      let pmData = []; //下午排班
      res.data.forEach((item) => {
        if (item.WORKTIME == '上午') {
          amData.push(item)
        } else if (item.WORKTIME == '下午') {
          pmData.push(item)
        }
      })
      this.setData({
        scheduleData: res.data,
        amData: amData,
        pmData: pmData,
        loading: false
      })
    })
  },
  // 获取医生列表
  getDoctor() {
    this.setData({
      loading: true
    })
    hospitalApi({
      method: 'getDoctor',
      data: JSON.stringify({
        hosCode: this.data.hosCode,
        depId: this.data.deptId,
        isWithIntro: '1',
        isWithSchedule: '1'
      })
    }).then(res => {
      this.setData({
        doctors: res.data,
        loading: false
      })
    })
  },
  // 获取医生的详细信息
  getDoctorInfo(item) {
    return new Promise((resolve, reject) => {
      if (item.RESERVETYPE == 2) {
        hospitalApi({
          method: 'getDoctor',
          data: JSON.stringify({
            hosCode: this.data.hosCode,
            depId: this.data.deptId,
            docId: item.DOCID,
          })
        }).then(res => {
          resolve(res.data[0])
        })
      } else {
        resolve([])
      }
    })
  },
  // 跳转到医生排版的页面
  toPage(e) {
    const item = e.mark.item;
    if (item.DOCID) {
      wx.setStorageSync('doctor', item); //保存选中的医生信息
      wx.navigateTo({
        url: `/pages/medical/schedule/index?deptId=${this.data.deptId}&hosCode=${this.data.hosCode}&docId=${item.DOCID}`
      })
    } else {
      wx.showToast({
        title: '医生代码错误！',
        icon: 'error'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})